from django.shortcuts import render
from rest_framework.response import Response
from rest_framework.views import APIView
# Create your views here.
from course import models,course_serializer

class HomeCate(APIView):
    def get(self,request):
        allcate=models.Category.objects.filter(parent=None,flag=True).order_by('order')

        ser=course_serializer.Categoryserializer(allcate,many=True)
        data=ser.data
        for item in data:
           item['category_set']=item['category_set'][:3]
        return Response({"code":200,"msg":"ok","data":data})

class HomeChile(APIView):
    def get(self, request):
        cate = request.query_params.get('cate', '')
        if not cate:
            return Response("参数不对")
        catelist = models.Category.objects.get(id=cate, flag=True)
        ser = course_serializer.Categoryserializer(catelist)
        data = ser.data

        print(data)
        child=[]
        for item in data['category_set']:
            child.append(item['id'])
        course=models.Course.objects.filter(cate__in=child,flag=True,ishome=True).order_by('order')[:4]
        couser=course_serializer.CourseSerializer(course,many=True)
        data['course']=couser.data
        return Response({'code':200,'msg':'ok','data':data})


class ParetCate(APIView):
    def get(self,request):
        cate=models.Category.objects.filter(parent=None,flag=True).order_by('order')
        #序列化器
        ser=course_serializer.Categoryser(cate,many=True)

        return Response({'code':200,'msg':'ok','data':ser.data})




class ChildCate(APIView):
    def get(self,request):
        parent=request.query_params.get('parent',None)
        if not parent:
            return Response("参数不对")
        if parent=='0':
            cate=models.Category.objects.exclude(parent=None,flag=True).order_by('order')
        else:
            cate=models.Category.objects.filter(parent=parent,flag=True).order_by('order')

        ser=course_serializer.Categoryser(cate,many=True)
        return Response({'code':200,'data':ser.data})
from rest_framework.pagination import  PageNumberPagination
from  rest_framework.generics import  ListAPIView
from django.db.models import Q


class MyPage(PageNumberPagination):
    page_size_query_param = 'size'


class ListView(ListAPIView):
    queryset = models.Course.objects.all()
    serializer_class = course_serializer.CourseSerializer
    pagination_class = MyPage
    def get_queryset(self):
        direction=self.request.query_params.get('direction',None)#方向
        category=self.request.query_params.get("category",None)#分类
        level=self.request.query_params.get('level',None)#难度
        ordering=self.request.query_params.get('ordering',None)#排序
        query=Q(flag=True)##默认条件是
        #点击二级分类
        if category and category!="0":
            query&=Q(cate=category)
        #点击一级分类
        if direction!='0' and category=="0":
            alichild=models.Category.objects.filter(parent=direction).values_list('id',flat=True)
            query&=Q(cate__in=alichild)#没看懂
        if level:
            query&=Q(level==level)
        if ordering:
            self.queryset=self.queryset.order_by(ordering)


        self.queryset=self.queryset.filter(query)

        return self.queryset



from drf_haystack.viewsets import HaystackViewSet
class  Search(HaystackViewSet):
    index_models=[models.Course]#mysql的模型类
    serializer_class=course_serializer.CourseIndexSerializer# 索引的序列化器类
    pagination_class = MyPage